package api

import (
	"github.com/valyala/fasthttp"
	"strings"
	"permission/user_info"
	"strconv"
)

// 清除角色缓存
func (bs *BaseServer) DelCacheRole(ctx *fasthttp.RequestCtx) {
	qArgs := ctx.QueryArgs()
	roleId, _  := strconv.Atoi(strings.Trim(string(qArgs.Peek("roleId")), " "))
	refresh := strings.Trim(string(qArgs.Peek("refresh")), " ")
	user_info.RefreshCacheRole(int64(roleId), refresh)
	ctx.Write([]byte("{\"status\":0, \"info\":\"success\"}"))
	return
}

// 清除部门缓存
func (bs *BaseServer) DelCacheDepart(ctx *fasthttp.RequestCtx) {
	qArgs := ctx.QueryArgs()
	refresh := strings.Trim(string(qArgs.Peek("refresh")), " ")
	user_info.RefreshCacheDepart(refresh)
	ctx.Write([]byte("{\"status\":0, \"info\":\"success\"}"))
	return
}

// 刷新缓存
func (bs *BaseServer) RefreshCache(ctx *fasthttp.RequestCtx) {
	bs.construct(ctx)

	qArgs := ctx.QueryArgs()
	userId, _ := strconv.Atoi(strings.Trim(string(qArgs.Peek("userId")), " "))
	tag := strings.Trim(string(qArgs.Peek("tag")), " ")
	token := strings.Trim(string(qArgs.Peek("token")), " ")
	if tag == "all" {
		if token != "cZd8NYAy95hdUyzb" {
			return
		}
	}
	user_info.RefreshCache(int64(userId), tag)
	ctx.Write([]byte("{\"status\":0, \"info\":\"success\"}"))
	return
}

